package org.example.BinaryTree;

import java.util.ArrayList;
import java.util.List;

public class Sequence_traversal_of_binary_trees_II {
    public static ArrayList<List<Integer>> arrayList = new ArrayList<>();
    public static void main(String[] args) {
        //二叉树的层序遍历 II

        //给你二叉树的根节点 root ，返回其节点值 自底向上的层序遍历 。 （即按从叶子节点所在层到根节点所在的层，逐层从左向右遍历）
        TreeNode node1 = new TreeNode(5);
        TreeNode node2 = new TreeNode(4);
        TreeNode node3 = new TreeNode(3);
        TreeNode node4 = new TreeNode(2);
        TreeNode node5 = new TreeNode(1);
        TreeNode root = new TreeNode(0);
        root.left = node1;
        root.right = node2;
        node1.left = node3;
        node1.right =  node4;
        node2.left = node5;
        //checkFun01(root,0);
        //root=null;
        function1(root,0);
        ArrayList<List<Integer>> resList = new ArrayList<>();
        for (int i = arrayList.size()-1; i >= 0; i--) {
            resList.add(arrayList.get(i));
        }
        System.out.print("[ ");
        for (List<Integer> list :resList){
            System.out.print("[ ");
            for (Integer i : list) {
                System.out.print(i+" ");
            }
            System.out.print("] ");
        }
        System.out.print("]");
    }
    public static void function1(TreeNode root,int deep) {
        if (root==null) return;
        deep++;
        if (arrayList.size()<deep){
            ArrayList<Integer> item = new ArrayList<>();
            arrayList.add(item);
        }
        arrayList.get(deep-1).add(root.val);
        function1(root.left,deep);
        function1(root.right,deep);
    }
}
